﻿using System;
using System.IO;
using System.Web;
using System.Web.UI;
using MuzoBlog.Core.BussinessLogic.Settings;
using MuzoBlog.Core.BussinessLogic.Themes;
using MuzoBlog.Core.Helpers;

namespace MuzoBlog.Core.Web.Controls
{
    public class BasePage : System.Web.UI.Page
    {
        const string ViewStateKey = "__MuzoViewState";

        private string GetViewStateSessionKey()
        {
            string ViewStateSessionKey = "SessionID:{0}_Page:{1}_MuzoViewState:{2}";
            return string.Format(ViewStateSessionKey, Session.SessionID, base.ClientID, ViewStateKey);
        }

        public BasePage()
        {
            base.Title = "This message from basepage class!!";
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            #region SetMasterPage
            string blogTheme = "unembellished"; // SettingManager.GetStringValue("Theme");

            string directoryFullPath = string.Concat(
                                    "~/Themes/",
                                    blogTheme
                                  );

            // Set theme
            base.MasterPageFile = string.Concat(directoryFullPath, "/MasterPage.master");

            // Set theme info variable
            BaseMasterPage baseMasterPage = (BaseMasterPage)base.Master;
            baseMasterPage.ThemeInfo = ThemeManager.GetInfo(HttpContext.Current.Server.MapPath(string.Concat(directoryFullPath, "/info.xml")));

            #endregion
        }

        protected override object LoadPageStateFromPersistenceMedium()
        {
            if (SettingManager.GetBooleanValue("ViewState.Compress"))
            {
                string strViewState;
                if (SettingManager.GetBooleanValue("ViewState.MoveInSession"))
                {
                    if (Session[GetViewStateSessionKey()] == null)
                        throw new Exception("Muzo ViewState not found in current session");

                    strViewState = Convert.ToString(Session[GetViewStateSessionKey()]);
                }
                else
                {
                    if (Request.Form["__MuzoViewState"] == null)
                        throw new Exception("Muzo ViewState not found in current request");

                    strViewState = Request.Form[ViewStateKey];
                }

                byte[] data = CompressHelper.Decompress(Convert.FromBase64String(strViewState));

                LosFormatter losFormatter = new LosFormatter();
                return losFormatter.Deserialize(Convert.ToBase64String(data));
            }
            else
            {
                return base.LoadPageStateFromPersistenceMedium();
            }

        }

        protected override void SavePageStateToPersistenceMedium(object viewState)
        {
            if (SettingManager.GetBooleanValue("ViewState.Compress"))
            {
                LosFormatter losFormatter = new LosFormatter();
                StringWriter stringWriter = new StringWriter();
                losFormatter.Serialize(stringWriter, viewState);

                byte[] data = Convert.FromBase64String(stringWriter.ToString());
                data = CompressHelper.Compress(data);

                if (SettingManager.GetBooleanValue("ViewState.MoveInSession"))
                {
                    Session[GetViewStateSessionKey()] = Convert.ToBase64String(data);
                }
                else
                {
                    ClientScript.RegisterHiddenField(ViewStateKey, Convert.ToBase64String(data));
                }
            }
            else
            {
                base.SavePageStateToPersistenceMedium(viewState);
            }
        }
    }
}